﻿
Partial Class CartSummary
    Inherits System.Web.UI.UserControl

    Private pMode As Boolean
    Public Property ReadOnlyMode() As Boolean
        Get
            Return pMode
        End Get
        Set(ByVal value As Boolean)
            pMode = value
        End Set
    End Property
    Private pOverrideDS As String
    Public Property OverrideDS() As String
        Get
            Return pOverrideDS
        End Get
        Set(value As String)
            pOverrideDS = value
        End Set
    End Property
    Private pCartString As String
    Public Property CartString() As String
        Get
            Return pCartString
        End Get
        Set(value As String)
            pCartString = value
        End Set
    End Property
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If OverrideDS <> String.Empty Then
            gvwCartList.DataSourceID = pOverrideDS
        End If
        If Not Page.IsPostBack Then
            ShowCheckout()
        End If
    End Sub

    Protected Sub odsViewCart_Deleted(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles odsViewCart.Deleted
        CType(Page.Master.FindControl("lblCartSummary"), Label).Text = CType(Session(CartString), CartProducts).GetItemCount()
        ShowCheckout()
    End Sub

    Private Sub ShowCheckout()
        If CType(Session(CartString), CartProducts).GetItemCount() <= 0 Then
            btnCheckout.Visible = False AndAlso Not pMode
        Else
            btnCheckout.Visible = True AndAlso Not pMode
        End If
    End Sub
    Public Function GetSum() As Double
        Return CType(Session(CartString), CartProducts).GetCartSum()
    End Function
    Protected Sub chkPersonalize_Changed(sender As Object, e As System.EventArgs)
        Dim chk As CheckBox = CType(sender, CheckBox)
        Dim txt As TextBox = CType(chk.Parent.FindControl("txtComment"), TextBox)
        If chk.Checked Then
            txt.Enabled = True
        Else
            txt.Text = String.Empty
            txt.Enabled = False
        End If

    End Sub
    Protected Sub btnCheckout_Click(sender As Object, e As System.EventArgs) Handles btnCheckout.Click
        SaveCart()
        'CType(Page.Master.FindControl("lblCartSummary"), Label).Text = CType(Session(CartString), CartProducts).GetItemCount()
        Response.Redirect("Checkout.aspx")
    End Sub
    Protected Sub btnUpdateTotal_Click(sender As Object, e As System.EventArgs)
        SaveCart()
        Response.Redirect("ViewCart.aspx")
    End Sub
    Private Sub SaveCart()
        'Loop through grid rows and update cart with any changes
        For Each row As GridViewRow In gvwCartList.Rows
            Dim prod As New Product()
            prod.RowNumber = Utility.GetValidInt(CType(row.FindControl("lblRow"), Label).Text)
            prod.Qty = Utility.GetValidInt(CType(row.FindControl("txtQty"), TextBox).Text)
            prod.Personalize = CType(row.FindControl("chkPersonalize"), CheckBox).Checked
            prod.Comment = Utility.GetValidString(CType(row.FindControl("txtComment"), TextBox).Text.Trim)
            UserCart.UpdateItem(prod)
        Next
    End Sub
End Class
